﻿using BrnMall.Core;
using BrnMall.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.Order
{
    public  partial class PrintRecords
    {
        public static int GetPrintOid(int storeid) {
            object obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.ExecuteScalar<object>("select top 1 oid from bma_orders where oid not in (select oid from bma_printrecord where printstate=1) and OrderState>=80 and storeid=@0",storeid);
            }
            return obj != null ? Convert.ToInt32(obj) : 0;
        }

        public static int InsertPrintRecord(PrintRecord pr)
        {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.Execute("insert into bma_PrintRecord values(@0,@1,@2,@3,getdate())", pr.StoreID, pr.Oid,pr.PrintID,pr.PrintState);
            }
            return obj;
        }

        public static int UpdatePrintRecord(PrintRecord pr)
        {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.Execute("update bma_PrintRecord set PrintState=@0 where StoreID=@1 and Oid=@2 and PrintID=@3",
                    pr.PrintState, pr.StoreID, pr.Oid, pr.PrintID);
            }
            return obj;
        }

        public static int GetStoreIDByUid(int Uid) {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.ExecuteScalar<int>("select storeid from bma_users where uid=@0",Uid);
            }
            return obj;
        }

        public static int GetPrintRecordsByOid(int StoreId, int Oid)
        {
            object obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.ExecuteScalar<object>("select top 1 oid from bma_PrintRecord where StoreID=@0 and Oid=@1", StoreId, Oid);
            }
            return obj != null ? Convert.ToInt32(obj) : 0;
        }
    }
}
